package com.power.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.power.domain.Member;
import com.power.model.Result;
import com.power.service.MemberService;
import com.power.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 微信小程序用户信息设置接口
 * @Author dingchunlin
 * @Date 2024/07/21 9:36
 * @Version 1.0.0
 */
@Api(tags = "微信小程序用户信息设置接口")
@RestController
@RequestMapping("/p/user")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 更新会员的头像和昵称
     * @param member 会员信息
     * @return 操作结果
     */
    @ApiOperation("更新会员的头像和昵称")
    @PutMapping("/setUserInfo")
    public Result<String> modifyMemberInfo(@RequestBody Member member){
        Boolean modify = memberService.modifyMemberInfoByOpenId(member);
        return Result.handle(modify);
    }

    /**
     * 查询会员是否绑定手机号
     * @return 操作结果
     */
    @ApiOperation(value = "查询会员是否绑定手机号")
    @GetMapping("/isBindPhone")
    public Result<Boolean> loadMemberIsBindPhone() {
        String openId = AuthUtils.getMemberOpenId();
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .eq(Member::getOpenId, openId));
        return Result.success(StringUtils.hasText(member.getUserMobile()));
    }

    /**
     * 根据openId列表查询会员列表
     * @param openIds 微信openId列表
     * @return 会员列表
     */
    @GetMapping("/getMemberListByOpenIds")
    @ApiOperation(value = "根据openId列表查询会员列表")
    Result<List<Member>> getMemberListByOpenIds(@RequestParam List<String> openIds){
        List<Member> memberList = memberService.list(new LambdaQueryWrapper<Member>()
                .in(Member::getOpenId, openIds)
        );
        return Result.success(memberList);
    }
}
